package com.msun.monitor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msun.monitor.entity.AlertLabel;
import com.msun.monitor.mapper.AlertLabelMapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = MsunMonitorApplicationTests.class)
public class AlertLabelAnaysis {

    @Resource
    AlertLabelMapper alertLabelMapper;

    @Test
    public void test() {


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("AlertId");

        Map<String, Map<String, Map<String, String>>> map = new HashMap<>();

        for (int i = 1; i <= 100; i++) {
            Page<AlertLabel> page = new Page<>(i, 100);
            System.out.println(i);
            Page<AlertLabel> selected = alertLabelMapper.selectPage(page, queryWrapper);
            List<AlertLabel> all = selected.getRecords();
            Integer alertId = all.get(0).getAlertId();
            Map<String, String> tmp = new HashMap<>();
            for (AlertLabel label : all) {
                if (alertId.intValue() != label.getAlertId().intValue()) {
                    push(map, tmp);
                    tmp.clear();
                    tmp = new HashMap<>();
                    alertId = label.getAlertId();
                }
                tmp.put(label.getLabel(), label.getValue());
            }
            System.out.println(JSON.toJSONString(map));
        }
    }


    private void push(Map<String, Map<String, Map<String, String>>> map, Map<String, String> tmp) {
        String alertGroup = tmp.get("alertgroup");
        String alertName = tmp.get("alertname");
        Map<String, Map<String, String>> groupMap = map.computeIfAbsent(alertGroup, s -> new HashMap<>());
        Map<String, String> computed = groupMap.computeIfAbsent(alertName, s -> new HashMap<>());
        computed.putAll(tmp);
    }
}
